<template>
	<view class="page">
		<!-- 搜索条件 -->
		<view class="search-box">
			<input class="search-input" v-model="searchParams.fCustName" placeholder="请输入购货单位" />
			<view class="form-actions">
				<button class="btn-search" @click="getCustomerList()">搜索</button>
				<button class="btn-reset" @click="reset()">重置</button>
			</view>
		</view>
		<!-- 列表 -->
		<view class="order-list">
			<view class="order-item" v-for="(item, index) in orderList" :key="index"
				:class="{ 'selected': selectedIndex === index }" @click="selectItem(index)">
				<view class="order-info">
					<text class="label">购货单位：</text>
					<text class="value">{{ item.fCustName }}</text>
				</view>
				<view class="order-info">
					<text class="label">客户电话：</text>
					<text class="value">{{ item.fTel}}</text>
				</view>
				<view class="order-info">
					<text class="label">交货地点：</text>
					<text class="value">{{ item.fAddress }}</text>
				</view>
			</view>
		</view>

		<!-- 确认按钮 -->
		<view class="confirm-bar" v-if="orderList.length > 0">
			<button class="btn-confirm" @click="confirmSelect">确认选择</button>
		</view>
	</view>
</template>

<script>
	import {
		customerList
	} from '@/api/saleOrder.js'

	export default {
		onUnload() {
			
			if(this.b){
				// 订单列表页点击返回 → 直接去工作台
				uni.navigateTo({
					url: '/pages/placeAnOrder/moveTheOrder'
				})
			}

		},
		data() {
			return {
				pageNum: 1,
				pageSize: 5,
				total: 0,
				searchParams: {
					fCustName: "",
					fTel: ""
				},

				orderList: [],
				selectedIndex: -1, // 当前选中的索引
				b: true
			}
		},
		created() {
			this.getCustomerList()
		},
		methods: {
			onReachBottom() {
				if (this.orderList.length < this.total) {
					this.getBottomCustomerList()
				} else {
					uni.showToast({
						title: '没有更多了',
						icon: 'none'
					})
				}
			},
			getCustomerList() {
				this.orderList = [];
				this.selectedIndex = -1;
				this.pageNum = 1;

				let thit = this
				uni.showLoading({
					title: '加载中...'
				}) // 显示加载弹窗
				thit.searchParams.pageNum = this.pageNum;
				thit.searchParams.pageSize = this.pageSize;
				customerList(
					thit.searchParams
				).then(res => {
					// 假设后端返回的数据格式是 { code: 200, data: [...] }
					if (res.code === 200) {
						thit.total = res.total
						thit.orderList = thit.orderList.concat(res.rows)
						thit.pageNum++
						thit.loading = false
						uni.hideLoading() // 关闭加载弹窗
					} else {
						console.log(res.msg + "失败")
						this.loading = false
						uni.hideLoading() // 关闭加载弹窗
					}
				}).catch(() => {

					this.loading = false
					uni.hideLoading()
				})

			},
			getBottomCustomerList() {

				this.selectedIndex = -1;
				this.pageNum = 1;
				let thit = this
				uni.showLoading({
					title: '加载中...'
				}) // 显示加载弹窗
				thit.searchParams.pageNum = this.pageNum;
				thit.searchParams.pageSize = this.pageSize;
				customerList(
					thit.searchParams
				).then(res => {
					// 假设后端返回的数据格式是 { code: 200, data: [...] }
					if (res.code === 200) {
						thit.total = res.total
						thit.orderList = thit.orderList.concat(res.rows)
						thit.pageNum++
						thit.loading = false
						uni.hideLoading() // 关闭加载弹窗
					} else {
						console.log(res.msg + "失败")
						this.loading = false
						uni.hideLoading() // 关闭加载弹窗
					}
				}).catch(() => {

					this.loading = false
					uni.hideLoading()
				})

			},

			reset() {

				this.orderList = [];
				this.selectedIndex = -1;
				this.pageNum = 1;
				this.searchParams.fCustName = "";
				this.getCustomerList()

			},
			selectItem(index) {
				this.selectedIndex = index
			},
			confirmSelect() {
				if (this.selectedIndex === -1) {
					uni.showToast({
						title: '请先选择一个客户',
						icon: 'none'
					})
					return
				}
				this.b = false

				const selectedCustomer = this.orderList[this.selectedIndex]
				// 跳转到指定页面，例如：/pages/order/createOrder
				// 注意：请根据你的实际页面路径修改
				uni.redirectTo({
					url: `/pages/placeAnOrder/moveTheOrder?data=${encodeURIComponent(JSON.stringify(selectedCustomer))}&source=customer`
				})

				// 如果你使用的是 params 传对象方式（某些框架支持），也可以用事件总线或 vuex
				// 这里推荐使用 query 传字符串（JSON 转 base64 或 encodeURIComponent）
			}
		}
	}
</script>

<style>
	.page {
		padding: 20rpx;
		background-color: #f5f5f5;
		min-height: 100vh;
	}

	.form-actions {
		display: flex;
		gap: 12px;
		margin-top: 10px;
	}

	.btn-search {
		flex: 1;
		height: 40px;
		background: #4caf50;
		color: #fff;
		border-radius: 8px;
		border: none;
		font-size: 15px;
	}

	.btn-reset {
		flex: 1;
		height: 40px;
		background: #f5f5f5;
		color: #333;
		border-radius: 8px;
		border: 1px solid #ddd;
		font-size: 15px;
	}

	/* 搜索框样式 */
	.search-box {
		display: flex;
		flex-direction: column;
		gap: 10px;
		margin: 10px;
		background: #fff;
		padding: 20rpx;
		border-radius: 16rpx;
	}

	.search-input {
		flex: 1;
		min-width: 180px;
		height: 40px;
		border: 1px solid #ddd;
		border-radius: 8px;
		padding: 0 10px;
		font-size: 14px;
		background-color: #fafafa;
	}

	/* 列表样式 */
	.order-list {
		display: flex;
		flex-direction: column;
		gap: 20rpx;
		margin: 10px;
	}

	.order-item {
		background: #fff;
		padding: 20rpx;
		border-radius: 16rpx;
		box-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.05);
	}

	/* 选中样式 */
	.order-item.selected {
		border: 2rpx solid #4caf50;
		position: relative;
	}

	.order-item.selected::after {
		content: '✓';
		position: absolute;
		top: 10rpx;
		right: 20rpx;
		color: #4caf50;
		font-weight: bold;
		font-size: 24rpx;
	}

	.order-info {
		display: flex;
		margin-bottom: 10rpx;
	}

	.order-info .label {
		width: 160rpx;
		font-weight: bold;
		color: #333;
	}

	.order-info .value {
		flex: 1;
		color: #666;
	}

	/* 确认按钮栏 */
	.confirm-bar {
		position: sticky;
		bottom: 0;
		background: #fff;
		padding: 20rpx;
		margin: 10px;
		border-radius: 16rpx;
		box-shadow: 0 -4rpx 10rpx rgba(0, 0, 0, 0.05);
	}

	.btn-confirm {
		width: 100%;
		height: 80rpx;
		background: #007AFF;
		color: #fff;
		border-radius: 8px;
		font-size: 16px;
		font-weight: bold;
	}
</style>